
package com.sun.showcase.biz.mall.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.showcase.biz.mall.dao.CollectionDao;
import com.sun.showcase.biz.mall.service.CollectionService;
import com.sun.showcase.client.domain.mall.Collection;
import com.sun.showcase.client.query.mall.CollectionQuery;
import com.sun.showcase.pojo.DataGrid;
import com.sun.showcase.pojo.Pager;
import com.sun.showcase.utils.ExecuteResult;
import com.sun.showcase.utils.UuidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("collectionService")
@Transactional
public class CollectionServiceImpl implements CollectionService{
	@Autowired
	private CollectionDao collectionDao;
	
	public void setCollectionDao(CollectionDao dao) {
		this.collectionDao = dao;
	}

	public DataGrid datagrid(CollectionQuery collectionQuery) {
		DataGrid j = new DataGrid();
		Pager<Collection> pager  = find(collectionQuery);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<CollectionQuery> getQuerysFromEntitys(List<Collection> collections) {
		List<CollectionQuery> collectionQuerys = new ArrayList<CollectionQuery>();
		if (collections != null && collections.size() > 0) {
			for (Collection tb : collections) {
				CollectionQuery b = new CollectionQuery();
				BeanUtils.copyProperties(tb, b);
				collectionQuerys.add(b);
			}
		}
		return collectionQuerys;
	}

	private Pager<Collection> find(CollectionQuery collectionQuery) {
		Page<Collection> page = PageHelper.startPage(collectionQuery.getPage().intValue(), collectionQuery.getRows().intValue());
		List<Collection> list = collectionDao.findList(collectionQuery);
		Pager<Collection> pager = new Pager<Collection>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public Collection add(CollectionQuery collectionQuery) {
		Collection t = new Collection();
		BeanUtils.copyProperties(collectionQuery, t);
		t.setCreateDate(new Date());
	    t.setId(UuidUtil.get32UUID());
		collectionDao.save(t);
		collectionQuery.setId(t.getId());
		return t;
		
	}

	public void update(CollectionQuery collectionQuery) {
		Collection t = collectionDao.getById(collectionQuery.getId());
	    if(t != null) {
	    	BeanUtils.copyProperties(collectionQuery, t);
	    	collectionDao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(CollectionQuery collectionQuery) {
	    collectionDao.updatePart(collectionQuery);
	}

	public void delete(String[] ids) {
		collectionDao.deleteAc(ids);
	}

	//逻辑删除 更新状态位
	public void deletePt(String[] ids) {
		collectionDao.deletePt(ids);
	}

	public Collection get(CollectionQuery collectionQuery) {
		return collectionDao.getById(collectionQuery.getId());
	}

	public Collection get(String id) {
		return collectionDao.getById(id);
	}

	
	public List<CollectionQuery> listAll(CollectionQuery collectionQuery) {
	    List<Collection> list = collectionDao.findList(collectionQuery);
		List<CollectionQuery> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}

	@Override
	public ExecuteResult<String> collect(CollectionQuery collectionQuery) {
		ExecuteResult<String> r = new ExecuteResult<String>();
		if(StringUtils.isEmpty(collectionQuery.getGoodsId())
				|| StringUtils.isEmpty(collectionQuery.getUserId())) {
			r.addErrorMessage("无效操作！");
			return r;
		}
		Collection c = this.getByUserAndGoods(collectionQuery);
		if(c == null) {
			this.add(collectionQuery);
			r.setSuccessMessage("收藏成功！");
		}else {
			String[] ids = {c.getId()};
			this.delete(ids);
			r.setSuccessMessage("取消收藏成功！");
		}
		return r;
	}

	@Override
	public Collection getByUserAndGoods(CollectionQuery collectionQuery) {
		return collectionDao.getByUserAndGoods(collectionQuery);
	}
	
	
}
